package com.dmall.graph.dfs.app;

import com.dmall.graph.WeightedGraph;

public class ConnectedComponent {

    private WeightedGraph G;
    private boolean[] visited;
    private int cccount = 0;

    public ConnectedComponent(WeightedGraph G) {
        this.G = G;
        this.visited = new boolean[G.V()];

        for (int i = 0; i < G.V(); i++) {
            if (!visited[i]) {
                dfs(i);
                cccount ++;
            }
        }
    }

    private void dfs(int v) {
        visited[v] = true;
        for (Integer w : G.adj(v)) {
            if (!visited[w]) {
                dfs(w);
            }
        }
    }

    public int count() {
        return cccount;
    }
}
